package com.stu.manager.organization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.manager.config.Result;
import com.stu.manager.handler.LoginHandler;
import com.stu.manager.organization.entity.SysDepartment;
import com.stu.manager.organization.entity.SysProfessional;
import com.stu.manager.organization.entity.dto.AddDepartmentDto;
import com.stu.manager.organization.entity.dto.DepartmentDto;
import com.stu.manager.organization.mapper.SysProfessionalMapper;
import com.stu.manager.organization.service.SysDepartmentServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-02-12
 */
@RestController
@RequestMapping("/api/department")
@CrossOrigin
public class SysDepartmentController {

    @Autowired
    private SysDepartmentServiceImpl departmentServiceImpl;

    @Resource
    private SysProfessionalMapper professionalMapper;

    @Autowired
    private LoginHandler loginHandler;
    @GetMapping("findAllDepartment")
    @ApiOperation("查询所有学院")
    public Result findAllDepartment(HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        List<SysDepartment> allDepartment = departmentServiceImpl.findAllDepartment();

        return Result.ok().data("departmentList",allDepartment);
    }


    @PostMapping("pageDepartment/{current}/{limit}")
    @ApiOperation("模糊查询加分页展示学院")
    public Result pageDepartment(@PathVariable Long current,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) DepartmentDto departmentDto,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Page<SysDepartment> pageDepartment = new Page<>(current, limit);
        QueryWrapper<SysDepartment> wrapper = new QueryWrapper<>();
        String departmentName = departmentDto.getDepartmentName();
        String departmentCode = departmentDto.getDepartmentCode();
        if (!StringUtils.isEmpty(departmentName)) {
            wrapper.like("department_name", departmentName);
        }
        if (!StringUtils.isEmpty(departmentCode)) {
            wrapper.like("department_code", departmentCode);
        }

        IPage<SysDepartment> sysDepartmentIPage = departmentServiceImpl.pageDepartment(pageDepartment, wrapper);
        List<SysDepartment> records = sysDepartmentIPage.getRecords();
        long total = sysDepartmentIPage.getTotal();

        return Result.ok().data("total", total).data("records", records);
    }

    @PostMapping("addDepartment")
    @ApiOperation("添加学院")
    public Result addDepartment(@RequestBody AddDepartmentDto addDepartmentDto,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = departmentServiceImpl.addDepartment(addDepartmentDto);
        return result;
    }

    @GetMapping("getDepartmentByCode/{departmentCode}")
    @ApiOperation("查询学院信息，修改回显")
    public Result getDepartmentByCode(@PathVariable String departmentCode,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        SysDepartment departmentByCode = departmentServiceImpl.getDepartmentByCode(departmentCode);
        return Result.ok().data("item", departmentByCode);
    }


    @PostMapping("updateDepartment")
    @ApiOperation("修改学院信息")
    public Result updateDepartment(@RequestBody SysDepartment sysDepartment,HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        Result result = departmentServiceImpl.updateDepartment(sysDepartment);
        return result;
    }


    @DeleteMapping("delDepartment/{departmentCode}")
    @ApiOperation("删除学院信息")
    public Result updateDepartment(@PathVariable String departmentCode, HttpServletRequest request) {
        Integer integer = loginHandler.verifyUser(request);
        if(integer > 0){
            return Result.ok();
        }
        QueryWrapper<SysDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("department_code", departmentCode);
        SysDepartment one = departmentServiceImpl.getOne(wrapper);
        String departmentName = one.getDepartmentName();
        QueryWrapper<SysProfessional> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("department_name",departmentName);
        Integer count = professionalMapper.selectCount(wrapper1);
        if(count>0){
            return Result.error().message("当前学院有下属专业，请先删除专业！");
        }
        boolean remove = departmentServiceImpl.remove(wrapper);
        if (remove) {
            return Result.ok().message("删除成功！");
        } else {
            return Result.error().message("删除失败！");
        }
    }

}

